package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"order_srv/appconfig"
)

var (
	DB  *gorm.DB
	err error
)

func Mysql() {
	appconf := appconfig.Conf.Mysql
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", appconf.User, appconf.Password, appconf.Hort, appconf.Port, appconf.Data)
	fmt.Println(dsn)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("mysql初始化失败")
	} else {
		fmt.Println("mysql初始化成功")
	}
	err = DB.AutoMigrate(&Goods{}, &Order{}, &OrderGoods{}, &User{})
	if err != nil {
		fmt.Println("数据迁移失败")
	} else {
		fmt.Println("数据迁移成功")
	}
}
